package com.maven.front.service.manageCourse;

import com.maven.front.bean.*;
import com.maven.front.dto.EduCourseDto;
import com.maven.front.mapper.EduCourseMapper;
import com.maven.front.mapper.EduCourseTeacherMapper;
import com.maven.front.mapper.EduTeacherMapper;
import com.maven.front.mapper.SysSubjectMapper;
import com.maven.front.util.BeanUtils;
import com.maven.front.util.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

//    课程管理模块：
//      课程管理小模块
//    课程管理： 给课程添加教师页面  可以调用教师表 select()方法
//    教师表 select方法，传递教师实体类，空则查询所有

@Slf4j
@Service
public class EduCourseService {

    @Resource
    private EduCourseMapper eduCourseMapper;
    @Resource
    private SysSubjectMapper sysSubjectMapper;
    @Autowired
    private FileUploadService fileUploadService;
    @Resource
    private EduCourseTeacherMapper eduCourseTeacherMapper;
    @Resource
    private EduTeacherMapper eduTeacherMapper;


    //    3) 课程管理：
//          课程显示以及课程查询
    public List<EduCourseDto> listAll(EduCourse eduCourse){
        List<EduCourseDto> listEduCourseDto = new ArrayList<EduCourseDto>();
        List<EduCourse> listEduCourse = eduCourseMapper.select(eduCourse);
        for (int i=0;i<listEduCourse.size();i++){
//            获取当前数据的专业名称
            String name = sysSubjectMapper.selectByPrimaryKey(listEduCourse.get(i).getSubjectLink()).getSubjectName();
            EduCourseDto eduCourseDto = new EduCourseDto();
//            将父类数据复制给子类数据
            BeanUtils.copy(listEduCourse.get(i), eduCourseDto);
//            将获取的名称赋值给子类的新属性(name)
            eduCourseDto.setName(name);
            listEduCourseDto.add(eduCourseDto);
        }
        if(CollectionUtils.isEmpty(listEduCourseDto)){
            return null;
        }else{
            return listEduCourseDto;
        }
    }

    //    创建课程时，需要先获取教师数据
    public Result selAllTeachaer(Integer id, String name){
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setIsStar(id);
        eduTeacher.setName(name);
        List<EduTeacher> list = eduTeacherMapper.select(eduTeacher);
        if (CollectionUtils.isEmpty(list)){
            return new Result(10005,"查询失败！", null);
        } else {
            return new Result(200, "查询成功！", list);
        }
    }



    //    课程管理：
//          创建课程，添加数据库
    public Result saveEduCourse(EduCourseDto eduCourseDto){
//        查询所有课程信息
        List<EduCourse> listCourse = eduCourseMapper.select(null);
//        循环所有课程信息,并判断名称是否相同
        for(int i=0; i<listCourse.size(); i++){
            if(listCourse.get(i).getCourseName().equals(eduCourseDto.getCourseName())){
                return new Result(10101, "当前课程名称已存在！", null);
            }
        }
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copy(eduCourseDto, eduCourse);
        eduCourse.setAddTime(new Date());       //添加时间(当前时间)
        eduCourse.setUpdateTime(new Date());    //修改时间(未进行修改操作时的修改时间为添加时间)
        int num = eduCourseMapper.insertSelective(eduCourse);    //课程添加操作
        if (num > 0){
            int courseId = eduCourse.getCourseId();//获取当前新增课程的ID
            List<EduCourseTeacher> list = new ArrayList<EduCourseTeacher>();
            String[] arr = eduCourseDto.getTid().split(",");
            for (int i=0; i<arr.length; i++){
                EduCourseTeacher eduCourseTeacher = new EduCourseTeacher();
                eduCourseTeacher.setCourseId(courseId);
                eduCourseTeacher.setTeacherId(Integer.valueOf(arr[i]));
                list.add(eduCourseTeacher);
            }
            eduCourseTeacherMapper.saveEduCouTea(list);//批量插入课程教师
            return new Result(200, "新增课程成功！", courseId);
        }else {
            return new Result(10006, "新增课程失败！", null);
        }
    }



    //    选择教师完成，将选择的教师显示在创建课程页面
    public Result getTeacher(String tid){
        String [] arr = tid.split(",");
        List<EduTeacher> list = new ArrayList<EduTeacher>();
        for (int i=0; i<arr.length; i++){
            EduTeacher eduTeacher = new EduTeacher();
            if(arr[i]!=null){
                eduTeacher = eduTeacherMapper.getTeacher(Integer.valueOf(arr[i]));
                list.add(eduTeacher);
            }
        }
        if (CollectionUtils.isEmpty(list)){
            return new Result(10005, "查询失败！", null);
        } else {
            return new Result(200, "查询成功！", list);
        }
    }

    //    添加课程时，需要在教师与课程的中间表中添加数据
    public Result saveEduOurseTeacher(Integer courseId, String teaid){
        List<EduCourseTeacher> list = new ArrayList<EduCourseTeacher>();
        String[] arr = teaid.split(",");
        for (int i=0; i<arr.length; i++){
            EduCourseTeacher eduCourseTeacher = new EduCourseTeacher();
            eduCourseTeacher.setCourseId(courseId);
            eduCourseTeacher.setTeacherId(Integer.valueOf(arr[i]));
            list.add(eduCourseTeacher);
        }
        int num = eduCourseTeacherMapper.saveEduCouTea(list);
        if(num > 0){
            return new Result(10005, "失败！");
        } else {
            return new Result(200, "成功！");
        }
    }


    //    根据课程ID获取课程表中的信息
    public EduCourseDto getById(int courseId){
        EduCourse eduCourse = eduCourseMapper.selectByPrimaryKey(courseId);
        EduCourseDto eduCourseDto = new EduCourseDto();
        BeanUtils.copy(eduCourse, eduCourseDto);
//        获取专业名称
        String name = sysSubjectMapper.selectByPrimaryKey(eduCourse.getSubjectId()).getSubjectName();
        eduCourseDto.setName(name);
        return eduCourseDto;
    }


    //    根据课程ID获取课程所有的信息
    public List<Object> getAllCourse(int courseId){
//        获取要修改的课程数据
        EduCourse eduCourse = eduCourseMapper.selectByPrimaryKey(courseId);
//        用来保存课程教师信息
        List<EduTeacher> listTeacher = new ArrayList<EduTeacher>();
//        获取要修改的课程教师信息
        List<Integer> list = eduCourseTeacherMapper.selectByCourseId(courseId);
//        循环获取教师信息，将教师信息放入map集合中
        for (Integer l:list){
            EduTeacher eduTeacher = new EduTeacher();
            String name = eduTeacherMapper.selectByPrimaryKey(l).getName();
            eduTeacher.setId(l);
            eduTeacher.setName(name);
            listTeacher.add(eduTeacher);
        }
//        获取所有专业父节点
        List<SysSubject> listSubject = sysSubjectMapper.select();
        List<Object> listAll = new ArrayList<Object>();
        listAll.add(eduCourse);
        listAll.add(listTeacher);
        listAll.add(listSubject);
        return listAll;
    }

    //    修改数据前，需要先获取当前课程的教师数据
    public Result editGetTeacher(Integer courseId){
//        获取当前课程的所有教师ID
        List<EduCourseTeacher> list = eduCourseTeacherMapper.getCourseId(courseId);
        List<EduTeacher> listt = new ArrayList<EduTeacher>();
        for (EduCourseTeacher l:list){
//            通过教师ID获取当前教师信息
            EduTeacher eduTeacher = eduTeacherMapper.getTeacher(l.getTeacherId());
            listt.add(eduTeacher);
        }
        if (CollectionUtils.isEmpty(listt)){
            return new Result(10006, "查询失败！", null);
        } else {
            return new Result(200, "查询成功！", listt);
        }
    }

    //    修改课程信息
    public Result upCourse(EduCourseDto eduCourseDto){
//        查询所有课程信息
        List<EduCourse> listCourse = eduCourseMapper.select(null);
//        循环所有课程信息,并判断名称是否相同
        for(int i=0; i<listCourse.size(); i++){
            if(listCourse.get(i).getCourseName().equals(eduCourseDto.getCourseName()) && listCourse.get(i).getCourseId()!=eduCourseDto.getCourseId()){
                return new Result(10101, "当前课程明名称已存在！", null);
            }
        }
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copy(eduCourseDto, eduCourse);
        eduCourse.setUpdateTime(new Date());       //添加时间(当前时间)
        int num = eduCourseMapper.updateByPrimaryKeySelective(eduCourse);    //课程添加操作
        if (num > 0){
            int courseId = eduCourseDto.getCourseId();//获取当前课程的ID
            List<EduCourseTeacher> list = new ArrayList<EduCourseTeacher>();
            String[] arr = eduCourseDto.getTid().split(",");
            for (int i=0; i<arr.length; i++){
                EduCourseTeacher eduCourseTeacher = new EduCourseTeacher();
                eduCourseTeacher.setCourseId(courseId);
                eduCourseTeacher.setTeacherId(Integer.valueOf(arr[i]));
                list.add(eduCourseTeacher);
            }
//            给当前课程批量插入教师数据前要先删除当前课程的教师
            int count = eduCourseTeacherMapper.deleteByPrimaryKey(courseId);
//          删除完成后，在给当前课程重新添加教师数据
            if (count>0){
                eduCourseTeacherMapper.saveEduCouTea(list);
                return new Result(200, "新增课程成功！", courseId);
            } else {
                return new Result(10007,"新增数据失败！", null);
            }
        }else {
            return new Result(10006, "新增课程失败！", null);
        }
    }



    //         删除课程，逻辑删除( 修改状态 )
    public Result deleteCourseId(int courseId){
        EduCourse eduCourse = new EduCourseDto();
        eduCourse.setCourseId(courseId);
        eduCourse.setIsAvaliable(3);
        int num = eduCourseMapper.updateByPrimaryKeySelective(eduCourse);
        if (num > 0){
            return new Result(200, "删除成功！", null);
        } else {
            return new Result(10005, "删除失败！", null);
        }
    }

}
